package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.SystemMenuButtonDao;
import com.example.child_protection.dao.SystemMenuDao;
import com.example.child_protection.dao.SystemRoleButtonDao;
import com.example.child_protection.dao.SystemRoleMenuDao;
import com.example.child_protection.dto.base.BaseResponseDTO;
import com.example.child_protection.dto.info.SystemRoleMenuDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.add.RoleMeunAndButtonInsertRequestDTO;
import com.example.child_protection.dto.request.add.SystemRoleMenuInsertRequestDTO;
import com.example.child_protection.dto.request.update.SystemRoleMenuUpdateRequestDTO;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.enums.SexEnums;
import com.example.child_protection.model.SystemRoleButtonModel;
import com.example.child_protection.model.SystemRoleMenuModel;
import com.example.child_protection.model.SystemRoleModel;
import com.example.child_protection.service.SystemRoleMenuManageService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 角色菜单信息管理service实现层
 * @date 2018-11-2
 * @author Biz
 */
@Component
public class SystemRoleMenuManageServiceImpl extends MyBaseService implements SystemRoleMenuManageService {

    @Autowired
    public SystemRoleMenuDao systemRoleMenuDao;

    @Autowired
    public SystemMenuDao systemMenuDao;

    @Autowired
    public SystemMenuButtonDao systemMenuButtonDao;

    @Autowired
    public SystemRoleButtonDao systemRoleButtonDao;

    /**
     * 添加
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO insert(SystemRoleMenuInsertRequestDTO requestDTO){
        return this.service(ServiceActionEnums.SYSTEM_ROLE_MENU_INSERT,requestDTO, BaseResponseDTO.class, new MyServiceExecutor<SystemRoleMenuInsertRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(SystemRoleMenuInsertRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                SystemRoleMenuModel model = new SystemRoleMenuModel();
                BeanUtils.copyProperties(requestDTO,model);
                systemRoleMenuDao.insert(model);
                res.setId(model.getId());
                res.successfully();
                return res;
            }
        });
    }

    /**
     * 修改
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO update(SystemRoleMenuUpdateRequestDTO requestDTO){
        return this.service(ServiceActionEnums.SYSTEM_ROLE_MENU_UPDATE,requestDTO, BaseResponseDTO.class, new MyServiceExecutor<SystemRoleMenuUpdateRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(SystemRoleMenuUpdateRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                SystemRoleMenuModel model = new SystemRoleMenuModel();
                BeanUtils.copyProperties(requestDTO,model);
                systemRoleMenuDao.updateSelective(model);
                res.setId(model.getId());
                res.successfully();
                return res;
            }
        });
    }

    /**
     * 删除
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO delete(SimpleIdRequestDTO requestDTO){
        return this.service(ServiceActionEnums.SYSTEM_ROLE_MENU_DELETE, requestDTO, BaseResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                SystemRoleMenuModel model = new SystemRoleMenuModel();
                BeanUtils.copyProperties(requestDTO,model);
                systemRoleMenuDao.deleteForLogic(model);
                res.setId(requestDTO.getId());
                res.successfully();
                return res;
            }
        });
    }

    /**
     * 添加角色权限
     * @param requestDTO
     * @return
     */
    public BaseResponseDTO insertRoleMenuAndButton(RoleMeunAndButtonInsertRequestDTO requestDTO){
        return this.service(ServiceActionEnums.SYSTEM_ROLE_MENU_BUTTON_INSERT, requestDTO, BaseResponseDTO.class, new MyServiceExecutor<RoleMeunAndButtonInsertRequestDTO, BaseResponseDTO>() {
            @Override
            protected BaseResponseDTO myprocess(RoleMeunAndButtonInsertRequestDTO requestDTO) throws Exception {
                BaseResponseDTO res = new BaseResponseDTO();
                Long roleId = requestDTO.getRoleId();
                Long userId = requestDTO.getCreatePersonId();
                //删除菜单权限
                SystemRoleMenuModel roleMenuModel = new SystemRoleMenuModel();
                roleMenuModel.setRoleId(roleId);
                roleMenuModel.setLastUpdatePersonId(userId);
                systemRoleMenuDao.deleteByRoleId(roleMenuModel);
                //删除按钮权限
                SystemRoleButtonModel roleButtonModel = new SystemRoleButtonModel();
                roleButtonModel.setRoleId(roleId);
                roleButtonModel.setLastUpdatePersonId(userId);
                systemRoleButtonDao.deleteByRoleId(roleButtonModel);


                //添加菜单权限
                List<SystemRoleMenuModel> menus = JSONObject.parseArray(requestDTO.getMenuValues(), SystemRoleMenuModel.class);
                for(SystemRoleMenuModel menu :menus){
                    menu.setRoleId(roleId);
                    //menu.setCode(systemMenuDao.findId(menu.getMenuId()).getCode());
                    menu.setCreatePersonId(userId);
                    menu.setLastUpdatePersonId(userId);
                    systemRoleMenuDao.insert(menu);
                }
                //添加按钮权限
                List<SystemRoleButtonModel> buttons = JSONObject.parseArray(requestDTO.getButtonValues(), SystemRoleButtonModel.class);
                for(SystemRoleButtonModel button :buttons){
                    button.setRoleId(roleId);
                    //menu.setCode(systemMenuDao.findId(menu.getMenuId()).getCode());
                    button.setCreatePersonId(userId);
                    button.setLastUpdatePersonId(userId);
                    systemRoleButtonDao.insert(button);
                }
                res.successfully();
                return res;
            }
        });
    }

}
